﻿using System;
using System.Collections.Generic;
using FrankJie.Model;
using FrankJie.IDAL;
using FrankJie.Cache;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace FrankJie.BLL
{
    public class Post
    {
        private readonly static IPost dal = DataAccess.CreatePost();

        /// <summary>
        /// 增加一篇文章
        /// </summary>
        /// <param name="postInfo">文章实体</param>
        /// <returns>返回文章ID</returns>
        public static int Add(PostInfo postInfo)
        {
            int id = dal.Add(postInfo);
            if (id > 0)
            {
                CacheStrategy.RemoveObject(CacheKey.POST_LIST);
            }
            return id;
        }

        /// <summary>
        /// 更新一篇文章
        /// </summary>
        /// <param name="postInfo">文章实体</param>
        /// <returns>返回是否成功</returns>
        public static bool Update(PostInfo postInfo)
        {
            bool isSuccess = dal.Update(postInfo);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.POST_LIST);
            }
            return isSuccess;
        }

        /// <summary>
        /// 更改文章状态
        /// </summary>
        /// <param name="id">文章id</param>
        /// <param name="Status">状态 PostStatus枚举</param>
        /// <returns>返回是否成功</returns>
        public static bool UpdateStatus(int id, int status)
        {
            bool isSuccess = dal.UpdateStatus(id, status);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.POST_LIST);
            }
            return isSuccess;
        }

        /// <summary>
		/// 删除一篇文章
		/// </summary>
        /// <param name="id">文章id</param>
        /// <returns>返回是否成功</returns>
        public static bool Delete(int id)
        {
            bool isSuccess = dal.Delete(id);
            if (isSuccess)
            {
                CacheStrategy.RemoveObject(CacheKey.POST_LIST);
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据文章id获取文章
        /// </summary>
        /// <param name="id">文章id</param>
        /// <returns>返回文章实体</returns>
        public static PostInfo GetPostById(int id)
        {
            List<PostInfo> postInfoList = new List<PostInfo>();
            postInfoList = (List<PostInfo>)CacheStrategy.GetObject(CacheKey.POST_LIST);
            if (postInfoList != null)
            {
                foreach (PostInfo postInfo in postInfoList)
                {
                    if (postInfo.Id == id)
                    {
                        return postInfo;
                    }
                }
            }
            return dal.GetPostById(id);
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <returns>返回文章集合</returns>
        public static List<PostInfo> GetList()
        {
            List<PostInfo> postInfoList = new List<PostInfo>();
            postInfoList = (List<PostInfo>)CacheStrategy.GetObject(CacheKey.POST_LIST);
            if (postInfoList == null)
            {
                postInfoList = dal.GetList();
                CacheStrategy.AddObjectWithDepend(CacheKey.POST_LIST, postInfoList, new string[] { CacheKey.CATEGORY_LIST,CacheKey.TAG_LIST });
            }
            return postInfoList;
        }

        /// <summary>
        /// 增加浏览数
        /// </summary>
        /// <param name="dic"></param>
        public static void AddViewCount(Dictionary<int, int> dic)
        {
            dal.AddViewCount(dic);
        }
    }
}
